<header class="header" style="height: 44px;">
    <div class="fix_nav">
        <div class="nav_inner">
            <a class="nav-left back-icon" href="<?=$backurl?>">返回</a>
            <div class="tit">订单支付</div>
        </div>
    </div>
</header>

<div class="container mb50">
    <div class="row">
        <ul class="list-group">
            <li class="list-group-item text-primary">订单编号：<?=$order['sn']?></li>
            <?php foreach ($order['products'] as $item):?>
            <li class="list-group-item hproduct clearfix ">
                <div class="list-group_12">
                    <div class="p-pic">
                        <a href="/product/detail/goods_id/<?=$item['product_id']?>.html">
                            <img src="<?=$item['product_thumb']?>" alt="<?= $item['product_name']?>" class="img-responsive">
                        </a>
                    </div>
                    <div class="p-info">
                        <a href="/product/detail/goods_id/<?=$item['product_id']?>.html">
                            <p class="p-title"><?= $item['product_name']?></p>
                            <p class="p-origin">
                                <em class="price">¥<?=price_format($item['price'])?></em>
                            </p>
                        </a>
                    </div>
                </div>
            </li>
            <?php endforeach;?>
        </ul>
        <div class="list-group">
            <p class="list-group-item text-primary">支付方式</p>
            <div class="list-group-item">
                <span class="black">微信支付：</span><img src="/v2/images/buy_wx.jpg">
                <div class="icheck pull-right">
                    <input type="radio" name="payment"  checked id="wxpay" value="wxpay">
                    <label for="wxpay">
                        <span></span>
                    </label>
                </div>
            </div>

            <div class="list-group-item">
                <span class="black">支付宝支付：</span><img src="/v2/images/buy_zfb.jpg">
                <div class="icheck pull-right">
                    <input type="radio" name="payment" id="alipay" value="alipay">
                    <label for="alipay">
                        <span></span>
                    </label>
                </div>
            </div>
        </div>

        <input type="hidden" name="order_sn" id="order_sn" value="<?=$order['sn']?>"/>
    </div>
</div>
<div class="fixed-foot">
    <div class="fixed_inner">
        <div class="pay-point black">
            实付款：<em class="red f22">￥<span id="totalPrice"><?=price_format($order['amount'])?></span></em>
        </div>
        <div class="buy-btn-fix">
            <a href="javascript:;" id="J_payBtn" class="btn btn-danger btn-buy">确认付款</a>
        </div>
    </div>
</div>
<script>
    //支付
    $('#J_payBtn').click(function(){
        var order_sn = $('#order_sn').val();
        var payment = $('input[name=payment]:checked').val();
        do_pay(order_sn,payment);
    });

    function do_pay(order_sn,payment)
    {
        var url = '/order/dopay/sn/'+order_sn+'/code/'+payment+'.html';
        if(payment != 'wxpay'){
            location.href='/order/dopay/sn/'+order_sn+'/code/'+payment+'.html';
        }else{

            showPageLoadingMsg('请稍候...');
            $.post(url+'?'+Math.random(),{}, function(res){
                $(".ui-loader").remove();
                if(typeof (res) == 'object')
                {
                    wx_pay(res,order_sn);
                } else {
                    floatNotify.simple(res.msg);
                }
            },'json');
        }
    }

    function wx_pay(res,order_sn)
    {
        WeixinJSBridge.invoke('getBrandWCPayRequest',{
            "appId":res.appId,
            "timeStamp":res.timeStamp,
            "nonceStr":res.nonceStr,
            "package":res.package,
            "signType":res.signType,
            "paySign":res.paySign
        },function(res){
            if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                showPageLoadingMsg('处理中...');

                setInterval(function(){check_pay(order_sn)},1000);
            } else {
                //location.href='/order/pay?order_sn='+order_sn;
            }
        });
    }

    function check_pay(order_sn){
        $.post('/order/checkpay/sn/'+order_sn +'?'+Math.random(), function(res){
            if(res.status == 1)
            {
                window.location.href='/payment/return/sn/'+order_sn;
            }
        },'json');
    }
</script>
